home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / What's New / Sample Code / Processes / ProcDoggie2.1b2 / UEvents.p < prev    next >
Encoding:
Text File  |  1997-11-03  |  10.7 KB  |  351 lines  |  [TEXT/CWIE]

  1. UNIT UEvents;
  2.  
  3. {-------------------------------------------------------------------------------
  4.     File:        UEvents.p
  5.  
  6.     Contains:    ProcDoggie event handling code.
  7.  
  8.     Written by:    Forrest Tanaka
  9.  
  10.     Copyright:    © 1988-1997 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.     You may incorporate this sample code into your applications without
  15.     restriction, though the sample code has been provided "AS IS" and the
  16.     responsibility for its operation is 100% yours.  However, what you are
  17.     not permitted to do is to redistribute the source as "DSC Sample Code"
  18.     after having made changes. If you're going to re-distribute the source,
  19.     we require that you make it clear in the source that the code was
  20.     descended from Apple Sample Code, but that you've made changes.
  21. --------------------------------------------------------------------------------
  22. #
  23. #    This file is responsible for responding to events.  The main event loop
  24. #    in ProcDoggie.p and calls the DoEvent routine for each event it gets.
  25. #
  26. -------------------------------------------------------------------------------}
  27. {[j=20/57/1$] Pasmat Options}
  28.  
  29. INTERFACE
  30.  
  31.     USES
  32.         Events;
  33.  
  34.     PROCEDURE DoEvent(anEvent: EventRecord);
  35.  
  36. IMPLEMENTATION    
  37.  
  38. (*******************************************************************************
  39. * Used Units
  40. *******************************************************************************)
  41.  
  42.     USES
  43.         AppleEvents
  44.         ,Fonts
  45.         ,ToolUtils
  46.         ,DiskInit
  47.         ,SegLoad
  48.         ,GestaltEqu
  49.         
  50.         (* Application *)
  51.         ,UGlobals
  52.         ,UEmergMem
  53.         ,UProcessUtils
  54.         ,UMenuHandler
  55.         ,UProcessGuts
  56.         ;
  57.  
  58.  
  59. (*******************************************************************************
  60. * Constants
  61. *******************************************************************************)
  62.  
  63.     CONST
  64.         kBecomingActive = TRUE; {Pass to DoActivateEvt; indicates becoming active}
  65.  
  66.  
  67. {$S Main}
  68. (*******************************************************************************
  69. * Public: DoWindowDrag
  70. *
  71. * A rectangle that covers all screen can be retrieved from the desktop region’s
  72. * rgnBBox.  The desktop region can be retrieved by calling GetGrayRgn.
  73. *******************************************************************************)
  74.  
  75.     PROCEDURE DoWindowDrag (anEvent:       EventRecord;
  76.                             clickedWindow: WindowPtr);
  77.  
  78.         VAR
  79.             dragBounds: Rect; {Window can be dragged over this rectangle}
  80.  
  81.     BEGIN
  82.         (* GetGrayRgn^^.rgnBBox covers the desktop over all screens *)
  83.         dragBounds := GetGrayRgn^^.rgnBBox;
  84.         DragWindow (clickedWindow, anEvent.where, dragBounds)
  85.     END;
  86.  
  87.  
  88. {$S Main}
  89. (*******************************************************************************
  90. * Public: DoContentClick
  91. *
  92. * As new kinds of windows are added to this application, this routine will have
  93. * to be able to detect the new kind of window and dispatch to the routine that
  94. * handles clicks in that kind of window.
  95. *******************************************************************************)
  96.  
  97.     PROCEDURE DoContentClick (anEvent:       EventRecord;
  98.                               clickedWindow: WindowPtr);
  99.  
  100.         VAR
  101.             currWindow: WindowPtr; {Pointer to the current front window}
  102.  
  103.     BEGIN
  104.         currWindow := FrontWindow;
  105.  
  106.         (* Clicked window not in front; activate it *)
  107.         IF currWindow <> clickedWindow THEN
  108.             SelectWindow (clickedWindow)
  109.         ELSE
  110.             IF IsProcessListWindow (clickedWindow) THEN
  111.                 ClickProcessListWindow (clickedWindow, anEvent)
  112.     END;
  113.  
  114.  
  115. {$S Main}
  116. (*******************************************************************************
  117. * Public: DoUpdateEvt
  118. *
  119. * As new kinds of windows are added to this application, this routine will have
  120. * to be able to detect the new kind of window and dispatch to the routine that
  121. * handles update events in that kind of window.
  122. *******************************************************************************)
  123.  
  124.     PROCEDURE DoUpdateEvt (anEvent: EventRecord);
  125.  
  126.         VAR
  127.             eventWindow: WindowPtr; {Pointer to the window to update}
  128.  
  129.     BEGIN
  130.         eventWindow := WindowPtr(anEvent.message);
  131.  
  132.         (* Update the window that needs it *)
  133.         SetPort (eventWindow);
  134.         BeginUpdate (eventWindow);
  135.         IF IsProcessListWindow (eventWindow) THEN
  136.             DrawProcessListWindow (eventWindow)
  137.         ELSE IF IsProcessInfoWindow (eventWindow) THEN
  138.             DrawProcessInfoWindow (eventWindow);
  139.         EndUpdate (eventWindow)
  140.     END;
  141.  
  142.  
  143. {$S Main}
  144. (*******************************************************************************
  145. * Public: DoActivateEvt
  146. *
  147. * As new kinds of windows are added to this application, this routine will have
  148. * to be able to detect the new kind of window and dispatch to the routine that
  149. * handles activate events in that kind of window.
  150. *******************************************************************************)
  151.  
  152.     PROCEDURE DoActivateEvt (eventWind:      WindowPtr;
  153.                              becomingActive: Boolean);
  154.  
  155.     BEGIN
  156.         IF IsProcessListWindow (eventWind) THEN
  157.             ActivateProcessListWindow (eventWind, becomingActive);
  158.     END;
  159.  
  160.  
  161. {$S Main}
  162. (*******************************************************************************
  163. * DoMouseDown - Mouse-down event dispatcher
  164. *
  165. * When a mouseDown event is received in the main event loop, this routine is
  166. * called to determine which area on the screens the mouseDown was, and to
  167. * dispatch to the appropriate routine to handle mouseDown events in that area.
  168. * The mouseDown event is passed in the anEvent parameter.
  169. *
  170. * See the UMenuHandler unit for routines that handle mouse-down events in the
  171. * menu bar, and the UWindowHandler unit for routines that handle mouse-down
  172. * events in the windows.
  173. *******************************************************************************)
  174.  
  175.     PROCEDURE DoMouseDown (anEvent: EventRecord);
  176.  
  177.         VAR
  178.             clickArea: Integer;   {Area of the screen that was clicked}
  179.             eventWind: WindowPtr; {Pointer the clicked window, if any}
  180.  
  181.     BEGIN
  182.         (* Find clicked area of screen or window *)
  183.         clickArea := FindWindow (anEvent.where, (*<*)eventWind);
  184.  
  185.         (* Jump to mouseDown-handling routine appropriate for screen area *)
  186.         CASE clickArea OF
  187.             inMenuBar:
  188.                 DoMenuChoice (MenuSelect (anEvent.where));
  189.             inContent:
  190.                 DoContentClick (anEvent, eventWind);
  191.             inGoAway:
  192.                 IF TrackGoAway (eventWind, anEvent.where) THEN
  193.                     DoWindowClose (eventWind);
  194.             inDrag:
  195.                 DoWindowDrag (anEvent, eventWind)
  196.             OTHERWISE
  197.                 (* do nothing *) ;
  198.         END
  199.     END;
  200.  
  201.  
  202. {$S Main}
  203. (*******************************************************************************
  204. * DoKeyDown - Key-down event dispatcher
  205. *
  206. * When a keyDown or autoKey event is received in the main event loop, this
  207. * routine is called to determine whether key is a command-key equivalent for a
  208. * menu item or not.  If the command key isn’t down, then the key stroke is
  209. * ignored.  Otherwise, MenuKey is called to get the menu ID and item number
  210. * of the menu item that corresponds to the command key, if any.  Then
  211. * DoMenuChoice is called to dispatch to the appropriate routine for the chosen
  212. * menu item.  The keyDown or autoKey event is passed in anEvent.
  213. *
  214. * See the UMenuHandler unit for routines that handle menu events.
  215. *******************************************************************************)
  216.  
  217.     PROCEDURE DoKeyDown (anEvent: EventRecord);
  218.  
  219.         VAR
  220.             theKey: Char; {ASCII code of key that was pressed}
  221.  
  222.     BEGIN
  223.         (* Get the ASCII code of the pressed key *)
  224.         theKey := CHR (BAND (anEvent.message, charCodeMask));
  225.  
  226.         (* If anEvent was keyDown and command key was down, it’s menu command *)
  227.         IF (anEvent.what = keyDown) AND (BAND (anEvent.modifiers, cmdKey) <> 0)
  228.                 THEN
  229.             DoMenuChoice (MenuKey (theKey))
  230.     END;
  231.  
  232.  
  233. {$S Main}
  234. (*******************************************************************************
  235. * DoDiskEvt - Handle a disk-insert event
  236. *
  237. * This routine is called whenever this application receives an event indicating
  238. * that a disk was inserted.  If the disk can’t be mounted, the message field of
  239. * the event reflects the error, and we call DIBadMount to allow the user to
  240. * format the disk.
  241. *******************************************************************************)
  242.  
  243.     PROCEDURE DoDiskEvt (anEvent: EventRecord);
  244.  
  245.         CONST
  246.             kSysAlertLeft = 80; {Left coord of DIBadMount alert in screen coords}
  247.             kSysAlertTop  = 80; {Top coord of DIBadMount alert in screen coords}
  248.  
  249.         VAR
  250.             cornerPoint: Point; {Top-left corner of DIBadMount alert}
  251.             error:       OSErr;
  252.  
  253.     BEGIN
  254.         IF HiWord (anEvent.message) <> noErr THEN
  255.             BEGIN
  256.                 SetPt ((*<*)cornerPoint, kSysAlertLeft, kSysAlertTop);
  257.                 error := DIBadMount (cornerPoint, anEvent.message)
  258.             END
  259.     END;
  260.  
  261.  
  262. {$S Main}
  263. (*******************************************************************************
  264. * Public: DoOSEvt
  265. *
  266. * When an OS Event is received, it can be a suspend or resume event.
  267. *******************************************************************************)
  268.  
  269.     PROCEDURE DoOSEvt (anEvent: EventRecord);
  270.  
  271.         VAR
  272.             eventWindow: WindowPtr; {Pointer to window being activated/deactivated}
  273.             osEvtKind:   Byte;      {Kind of OSEvt; mouse-moved or suspend/resume}
  274.  
  275.     BEGIN
  276.         (* Only care if anEvent is suspend/resume event *)
  277.         osEvtKind := BAND (BSR (anEvent.message, 24), $00FF);
  278.         IF osEvtKind = suspendResumeMessage THEN
  279.             BEGIN
  280.                 (* It’s a suspend/resume event; suspend or resume? *)
  281.                 eventWindow := FrontWindow;
  282.                 IF BAND (anEvent.message, 1) <> 0 THEN
  283.                     BEGIN
  284.                         (* Resume event; set the cursor and activate front window *)
  285.                         InitCursor;
  286.                         IF eventWindow <> NIL THEN
  287.                             DoActivateEvt (eventWindow, kBecomingActive);
  288.                     END
  289.                 ELSE
  290.                     BEGIN
  291.                         (* Suspend event; deactivate the front window *)
  292.                         IF eventWindow <> NIL THEN
  293.                             DoActivateEvt (eventWindow, NOT kBecomingActive);
  294.                     END
  295.             END
  296.     END;
  297.  
  298.  
  299. {$S Main}
  300. (*******************************************************************************
  301. * DoHighLevelEvent - Handle a high-level event
  302. *
  303. * This routine handles the high-level event specified by anEvent.  The only
  304. * high-level events that this application handles are AppleEvents, so I just
  305. * pass the high-level event to AEProcessAppleEvent.  AEProcessAppleEvent calls
  306. * the appropriate AppleEvent handler routine to handle that particular kind of
  307. * AppleEvent.
  308. *******************************************************************************)
  309.  
  310.     PROCEDURE DoHighLevelEvent (anEvent: EventRecord);
  311.  
  312.         VAR
  313.             error: OSErr;
  314.  
  315.     BEGIN
  316.         error := AEProcessAppleEvent (anEvent);
  317.     END;
  318.  
  319.  
  320. {$S Main}
  321. (*******************************************************************************
  322. * DoEvent - Do an event from the main event loop, and elsewhere.
  323. *
  324. *******************************************************************************)
  325.  
  326.     PROCEDURE DoEvent(anEvent: EventRecord);
  327.     BEGIN
  328.         CASE anEvent.what OF
  329.             mouseDown:
  330.                 DoMouseDown (anEvent);
  331.             keyDown, autoKey:
  332.                 DoKeyDown (anEvent);
  333.             updateEvt:
  334.                 DoUpdateEvt (anEvent);
  335.             diskEvt:
  336.                 DoDiskEvt (anEvent);
  337.             activateEvt:
  338.                 DoActivateEvt (WindowPtr(anEvent.message),
  339.                         BAND (anEvent.modifiers, activeFlag) <> 0);
  340.             osEvt:
  341.                 DoOSEvt (anEvent);
  342.             kHighLevelEvent:
  343.                 DoHighLevelEvent (anEvent)
  344.             OTHERWISE
  345.                 (* do nothing *) ;
  346.         END
  347.     END;
  348.  
  349.  
  350. END.
  351.